<template>
  <div class="app-container">
    <div class="app-form">
      <ele-form
        v-model="formData"
        v-bind="formConfig"
        :request-fn="handleRequest"
        @request-success="handleRequestSuccess"
      />
      <qcord />
    </div>
  </div>
</template>

<script>
import { getAuthUrl, getAuthWechat } from '../../wxmp/api/wx-account-fans'
import { registerBetaMiniprogram, registerMiniprogram, registerMiniprogramByOffiaccount } from '../api/register'
export default {
  data() {
    return {
      formData: {},
      formConfig: {
        formDesc: {
          registerType: {
            type: 'radio',
            label: '注册方式',
            isOptions: true,
            default: 1,
            options: [
              {
                'text': '快速注册',
                'value': 1
              },
              {
                'text': '注册试用小程序',
                'value': 2
              }
            ]
          },
          name: {
            type: 'input',
            label: (data) => data.registerType === 1 ? '企业名' : '小程序名称'
          },
          openid: {
            type: 'input',
            label: '申请人openid',
            vif: (data) => data.registerType === 2,
            slots: {
              'append': (h) => {
                return h('span', { class: 'text-pointer', on: {
                  click: () => {
                    this.getAuthWechat()
                  }
                }}, '微信授权获取')
              }
            }
          },
          code: {
            type: 'input',
            label: '企业代码',
            vif: (data) => data.registerType === 1
          },
          code_type: {
            type: 'radio',
            label: '企业代码类型',
            vif: (data) => data.registerType === 1,
            isOptions: true,
            options: [
              {
                'text': '统一社会信用代码（18 位）',
                'value': 1
              },
              {
                'text': '组织机构代码（9 位 xxxxxxxx-x）',
                'value': 2
              },
              {
                'text': '营业执照注册号(15 位)',
                'value': 3
              }
            ]
          },
          legal_persona_wechat: {
            type: 'input',
            label: '法人微信号',
            vif: (data) => data.registerType === 1
          },
          legal_persona_name: {
            type: 'input',
            label: '法人姓名',
            vif: (data) => data.registerType === 1
          },
          component_phone: {
            type: 'input',
            label: '第三方联系电话',
            vif: (data) => data.registerType === 1
          }
        }
      },
      order: [
      ]
    }
  },
  created() {
    if (this.$route.query.auth_code) {
      // 授权成功后，将auth_code传给后端
      getAuthWechat({ auth_code: this.$route.query.auth_code }).then(res => {
        console.log('fetchAuthWechat', res)
        this.$message.success('授权成功')
        this.getWeChatInfo()
      }).catch(err => {
        console.log('fetchAuthWechat-err', err)
        this.$message.error('授权失败')
      })
    } else {
      this.getWeChatInfo()
    }
  },
  methods: {
    getAuthWechat() {
      this.$message.success('微信授权事件')
      const fullUrl = window.location.href
      const redirect_url = encodeURIComponent(fullUrl)
      getAuthUrl({ redirect_url: redirect_url }).then((res) => {
        this.url = res.data.url
        window.open(res.data.url, '_self')
      }).catch((error) => {
        console.log(error)
        this.$message.error('获取授权失败')
        return Promise.reject(error)
      })
    },
    handleRequest(data) {
      console.log(data)
      switch (data.registerType) {
        case 1:
          registerMiniprogram(data).then((res) => {
            console.log(res)
            this.$message.success('发送失败')
          }).catch((error) => {
            console.log(error)
            // this.$message.error('发送失败')
            // return Promise.reject(error)
          })
          break
        case 2:
          registerMiniprogramByOffiaccount(data).then((res) => {
            console.log(res)
            this.$message.success('发送失败')
          }).catch((error) => {
            console.log(error)
            // this.$message.error('发送失败')
            // return Promise.reject(error)
          })
          break
        case 3:
          registerBetaMiniprogram(data).then((res) => {
            console.log(res)
            this.$message.success('发送失败')
            // return Promise.resolve()
          }).catch((error) => {
            console.log(error)
            // this.$message.error('发送失败')
            // return Promise.reject(error)
          })
          break
      }

      return Promise.resolve()
    },
    handleRequestSuccess() {
      // this.$message.success('发送成功')
    }
  }
}
</script>
